import { useState, useEffect } from 'react';
import { Toast } from 'antd-mobile';
import { Http } from '@/utils';

export type Options = {
  url: string;
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
  headers?: any;
  body?: any;
  watch?: any;
  setLoadning?: any;
  setResult?: any;
};

type Result = {
  result: [] | object | undefined;
  loading: boolean;
};

export default function useHttpHook({
  url,
  method = 'POST',
  headers,
  body = {},
  watch = [],
}: Options): any {
  const [result, setResult] = useState<Array<any>>();
  const [loading, setLoadning] = useState(true);

  useEffect(() => {
    Http({ url, method, headers, body, watch, setResult, setLoadning });
  }, watch);

  return [result, loading];
}
